﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using BUS;
using DAO;
using DTO;
using System.Collections.Generic;

public partial class MasterPage2 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((Session["UserOnline"]) == null)
        {
            btnquenmatkhau.Visible = false;
            btndangky.Visible = true;
            tbuser.Visible = true;
            tbmatkhau.Visible = true;
            btn_Login.Visible = true;
            btn_Logout.Visible = false;
            btndangkygianhang.Visible = false;
            txt_useronline.Visible = false;
            btnchucnangchugianhang.Visible = false;
            btnchucnangmod.Visible = false;
        }
        else if (Session["UserOnline"] != null)
        {
            btnquenmatkhau.Text = "Quản lý tài khoản";
            btndangky.Visible = false;
            tbuser.Visible = false;
            tbmatkhau.Visible = false;            
            btn_Login.Visible = false;
            btn_Logout.Visible = true;
            btndangkygianhang.Visible = false;
            btnchucnangchugianhang.Visible = false;
            btnchucnangmod.Visible = false;
            ThanhVienDTO thanhvien = new ThanhVienDTO();
            thanhvien = ThanhVienBUS.LayThanhVienTheoUserName(Session["UserOnline"].ToString());
            if(thanhvien.MaNhomThanhVien == 1)
                btndangkygianhang.Visible = true;
            if(thanhvien.MaNhomThanhVien == 2)
                btnchucnangmod.Visible = true;
            if (thanhvien.MaNhomThanhVien == 3)
                btnchucnangchugianhang.Visible = true;
            txt_useronline.Visible = true;
            txt_useronline.Text = "Xin chào," + Session["UserOnline"].ToString();
        }

        if (!IsPostBack)
        {
            List<string> ds = HangSanXuatBUS.LayDanhSachTenHangSanXuat();
            for (int i = 0; i < ds.Count; i++)
            {
                ddlDanhSachNhaSanXuat.Items.Add(ds[i]);
            }
            ddlDanhSachNhaSanXuat.SelectedIndex = 0;

            ddlGiaTu.Items.Add("500 ngàn");
            for (int i = 1; i <= 20; i++)
            {
                ddlGiaTu.Items.Add(i.ToString() + " Triệu");

                ddlGiaDen.Items.Add(i.ToString() + " Triệu");
            }
            ddlGiaTu.SelectedIndex = 0;
            ddlGiaDen.SelectedIndex = 0;
        }        
    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btn_Logout_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("~/Default.aspx");
    }
    protected void btn_Login_Click(object sender, EventArgs e)
    {
        String connectstring = ConfigurationManager.ConnectionStrings["I_Group_WebConnectionString"].ToString();
        SqlConnection sqlconnect = new SqlConnection(connectstring);
        sqlconnect.Open();

        string sql = "select Username from THANH_VIEN where Username = @user and Password = @pass";
        SqlCommand commandForCheckUser = new SqlCommand(sql, sqlconnect);
        
        commandForCheckUser.Parameters.AddWithValue("@user",tbuser.Text);
        commandForCheckUser.Parameters.AddWithValue("@pass",ThanhVienDAO.Encrypt(tbmatkhau.Text, true));
        commandForCheckUser.CommandType = CommandType.Text;
        string userIdSuccess = Convert.ToString(commandForCheckUser.ExecuteScalar());
        if (userIdSuccess != "")
        {
            Session["UserOnline"] = userIdSuccess;
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            txt_useronline.Visible = true;
            txt_useronline.Text = "Tên người dùng hoặc mật khẩu bạn đã nhập là không đúng!";
        }
    }
    protected void btnquenmatkhau_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/CapNhatTaiKhoan.aspx");
    }
    protected void btndangky_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/DangKy.aspx");
    }

    protected void btndangkygianhang_Click(object sender, EventArgs e)
    {
        ThanhVienDTO thanhvien = new ThanhVienDTO();
        thanhvien = ThanhVienBUS.LayThanhVienTheoUserName(Session["UserOnline"].ToString());
        thanhvien.MaNhomThanhVien = 3;
        bool result = ThanhVienBUS.CapNhatThanhVien(thanhvien);
        if (result == true)
        {
            btndangkygianhang.Visible = false;
            btnchucnangchugianhang.Visible = true;
        }
        else
        {
            return;
        }
    }

    protected void bttSearch_Click(object sender, EventArgs e)
    {
        string tensanpham = txtTenSanPham.Text;
        string giatu = ddlGiaTu.Text;
        string giaden = ddlGiaDen.Text;
        string tenhangsanxuat = ddlDanhSachNhaSanXuat.Text;
        ViewState["txtTenSanPham"] = tensanpham;
        string path = ResolveClientUrl("~/KetQuaTimKiem.aspx");
        Response.Redirect(path + "?tensanpham=" + tensanpham +
            "&giatu=" + giatu + "&giaden=" + giaden +
            "&hangsanxuat=" + tenhangsanxuat);
    }
    protected void btnchucnangchugianhang_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/DanhSachChucNangChuGianHang.aspx");
    }
    protected void btnchucnangmod_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/DanhSachChucNangMod.aspx");
    }
}
